package com.zhuge_new.Activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.MyWebChromeClientUtil;

/**
 * Created by lenovo on 2018-09-15.
 */

public class WebActivity extends BaseActivity implements View.OnClickListener {

    private WebView web_wv;
    private String title;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        Intent intent=getIntent();
        title = intent.getStringExtra("title");
        url = intent.getStringExtra("url");
        init_title();
    }

    @Override
    public void init_title() {
        super.init_title();
        ImageView iv_back=findViewById(R.id.iv_nav_back);
        TextView tv_title=findViewById(R.id.tv_nav_title);
        tv_title.setText(title);
        iv_back.setOnClickListener(this);
        web_wv = findViewById(R.id.web_wv);
        web_wv.setWebChromeClient(new MyWebChromeClientUtil(this));
        geturl();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(WebActivity.class);
                break;
        }
    }
    public void geturl(){

        web_wv.loadUrl(url);

        web_wv.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.e(url,"打印url");
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
            }


            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                if (request.isForMainFrame()){

                }
            }
        });
        WebSettings webSettings=web_wv.getSettings();

        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setLoadWithOverviewMode(true);

        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        webSettings.setDomStorageEnabled(true);
        webSettings.setGeolocationEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
        //图库选择上传

        if (requestCode == MyWebChromeClientUtil.FILECHOOSER_RESULTCODE&&
                intent!=null&&resultCode == RESULT_OK) {
            Uri[] uris = new Uri[1];
            uris[0] = intent.getData();
            MyWebChromeClientUtil.update(uris);
        }
        //拍照上传
        else if(requestCode == MyWebChromeClientUtil.FILECHOOSER_RESULTCODE&&
                intent==null&&resultCode == RESULT_OK){
            Uri[] uris = new Uri[1];
            uris[0] =MyWebChromeClientUtil.myImageUri;
            MyWebChromeClientUtil.update(uris);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        web_wv.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        web_wv.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        web_wv.destroy();
    }

}
